using Game.Core;
using Game.Main.Model;
using SimpleJSON;

namespace Game.Component
{
	public class PopcornCupItem : BaseItem
	{
		public enum State
		{
			Empty = 0,
			Full = 1
		}

		public State state;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			SetState((State)data.state);
		}

		public override bool CanPlaceItem(BaseItem item)
		{
			if (state == State.Full)
			{
				return false;
			}
			return base.CanPlaceItem(item);
		}

		protected override void OnItemTriggerEnter(ColliderData collData)
		{
			base.OnItemTriggerEnter(collData);
			if (state == State.Empty && collData.item.itemData.type == 9 && childItems.Count < 1)
			{
				GlobalEventSystem.Fire(new BaseEvent("game.remove_item", collData.item.archiveData.index));
				SetState(State.Full);
			}
		}

		public void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				if (state == State.Empty)
				{
					model.Find("Popcorn").gameObject.SetActive(false);
				}
				else
				{
					model.Find("Popcorn").gameObject.SetActive(true);
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
		}

		public override bool CanEat()
		{
			return state == State.Full;
		}
	}
}
